home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9207.ZIP / AVKCAPT.ZIP / MEMALLOC.C < prev    next >
C/C++ Source or Header  |  1992-03-11  |  6KB  |  239 lines

  1. //-------------------------------------------------------------------------
  2. //            ActionMedia II Programmer's Toolkit
  3. //            
  4. //            Windows Sample Code Shared Functions
  5. //
  6. // Module Name:    memalloc.c
  7. //
  8. // description:    Functions to allocate and free NEAR, FAR and HUGE blocks
  9. //                of memory.  These functions act as generic shells around
  10. //                GlobalAlloc/GlobalLock/GlobalFree and LocalAlloc/LocalLock/
  11. //                LocalFree functions.
  12. //
  13. //    Copyright Intel Corp. 1991, 1992
  14. //    All Rights Reserved.
  15. //
  16. //-------------------------------------------------------------------------
  17. //
  18. // Exported functions from this module:
  19. //
  20. //        MemAlloc
  21. //        MemAllocNear
  22. //        MemAllocHuge
  23. //        MemFree
  24. //        MemFreeNear
  25. //        MemFreeHuge
  26. //
  27. //
  28. //-------------------------------------------------------------------------
  29. //    NOTES ON MemAlloc
  30. //
  31. //    The allocation functions take care of allocating NEAR, FAR and HUGE 
  32. //    blocks of memory, locking the blocks and returning pointers dereferenced
  33. //    from the Windows handles returned by the Windows allocation functions.
  34. //    The free functions extract the handles from the pointers and free 
  35. //    the blocks.
  36. //-------------------------------------------------------------------------
  37.  
  38.  
  39. #include <windows.h>
  40.  
  41. #define    L_FLAGS        LMEM_FIXED | LMEM_NODISCARD | LMEM_ZEROINIT
  42. #define    G_FLAGS        GMEM_MOVEABLE | GMEM_NODISCARD | GMEM_ZEROINIT
  43.  
  44. char far    *MemAlloc(WORD);
  45. char huge     *MemAllocHuge(unsigned long);
  46.  
  47. //-------------------------------------------------------------------------     
  48. //FUNCTION:                                                                     
  49. //    
  50. //    char far *MemAlloc(Size)
  51. //
  52. //PARMS IN:
  53. //
  54. //    WORD    Size        the size of the block to be allocated.
  55. //    
  56. //DESCRIPTION:
  57. //
  58. //    Uses Windows Global allocation functions to allocate Size bytes (or
  59. //    more) of memory.  If the allocation is successful, the allocated
  60. //    block is locked and the resulting pointer is returned to the caller.
  61. //    
  62. //RETURN:
  63. //
  64. //    32-bit far pointer to an allocated and locked block of at least Size 
  65. //    bytes. NULL if unsuccessful.
  66. //    
  67. //-------------------------------------------------------------------------
  68.  
  69. char far *
  70. MemAlloc(WORD Size)
  71. {
  72.     HANDLE         h;
  73.     char far     *p = NULL;
  74.  
  75.     if ((h = GlobalAlloc(G_FLAGS, (unsigned long)Size)) != (HANDLE)NULL)
  76.     {
  77.         if ((p = (char far *)GlobalLock(h)) == (char far *)NULL)
  78.             GlobalFree(h);
  79.     }
  80.     return p;
  81. }
  82.  
  83. //-------------------------------------------------------------------------     
  84. //FUNCTION:                                                                     
  85. //    
  86. //    char huge *MemAllocHuge(Size)
  87. //
  88. //PARMS IN:
  89. //
  90. //    unsigned long    Size        the size of the block to be allocated.
  91. //    
  92. //DESCRIPTION:
  93. //
  94. //    Uses Windows Global allocation functions to allocate Size bytes (or
  95. //    more) of memory.  If the allocation is successful, the allocated
  96. //    block is locked and the resulting pointer is returned to the caller.
  97. //    
  98. //RETURN:
  99. //
  100. //    32-bit huge pointer to an allocated and locked block of at least Size 
  101. //    bytes. NULL if unsuccessful.
  102. //    
  103. //-------------------------------------------------------------------------
  104.  
  105.  
  106. char huge *
  107. MemAllocHuge(unsigned long Size)
  108. {
  109.     HANDLE         h;
  110.     char huge    *p = NULL;
  111.  
  112.     if ((h = GlobalAlloc(G_FLAGS, (unsigned long)Size)) 
  113.       != (HANDLE)NULL)
  114.     {
  115.         if ((p = (char huge *)GlobalLock(h)) == (char huge *)NULL)
  116.             GlobalFree(h);
  117.     }
  118.     return p;
  119. }
  120.  
  121. //-------------------------------------------------------------------------     
  122. //FUNCTION:                                                                     
  123. //    
  124. //    char near *MemAllocNear(Size)
  125. //
  126. //PARMS IN:
  127. //
  128. //    WORD    Size        the size of the block to be allocated.
  129. //    
  130. //DESCRIPTION:
  131. //
  132. //    Uses Windows Local allocation functions to allocate Size bytes (or
  133. //    more) of memory.  If the allocation is successful, the allocated
  134. //    block is locked and the resulting pointer is returned to the caller.
  135. //    
  136. //RETURN:
  137. //
  138. //    16-bit near pointer to an allocated and locked block of at least Size 
  139. //    bytes. NULL if unsuccessful.
  140. //    
  141. //-------------------------------------------------------------------------
  142.  
  143.  
  144. char near *
  145. MemAllocNear(WORD Size)
  146. {
  147.     HANDLE         h;
  148.     char near     *p = NULL;
  149.  
  150.     if ((h = LocalAlloc(L_FLAGS, Size)) != (HANDLE)NULL)
  151.     {
  152.         if ((p = (char near *)LocalLock(h)) == (char near *)NULL)
  153.             LocalFree(h);
  154.     }
  155.     return p;
  156. }
  157.  
  158.  
  159. //-------------------------------------------------------------------------     
  160. //FUNCTION:                                                                     
  161. //    
  162. //    void MemFree(pBlock)
  163. //
  164. //PARMS IN:
  165. //
  166. //    char far *pBlock    pointer to block of memory to be freed.
  167. //    
  168. //DESCRIPTION:
  169. //
  170. //    Uses Windows Global allocation functions to unlock and free an 
  171. //    allocated block of memory.  
  172. //    
  173. //-------------------------------------------------------------------------
  174.  
  175. void
  176. MemFree(char far *p)
  177. {
  178.     HANDLE        h;
  179.  
  180.     h = LOWORD(GlobalHandle(HIWORD((unsigned long)p)));
  181.     GlobalUnlock(h);
  182.     GlobalFree(h);
  183. }
  184.  
  185.  
  186. //-------------------------------------------------------------------------     
  187. //FUNCTION:                                                                     
  188. //    
  189. //    void MemFreeHuge(pBlock)
  190. //
  191. //PARMS IN:
  192. //
  193. //    char huge *pBlock    pointer to block of memory to be freed.
  194. //    
  195. //DESCRIPTION:
  196. //
  197. //    Uses Windows Global allocation functions to unlock and free an 
  198. //    allocated block of memory.  
  199. //    
  200. //-------------------------------------------------------------------------
  201.  
  202. void
  203. MemFreeHuge(char huge *p)
  204. {
  205.     HANDLE        h;
  206.  
  207.     h = LOWORD(GlobalHandle(HIWORD((unsigned long)p)));
  208.     GlobalUnlock(h);
  209.     GlobalFree(h);
  210. }
  211.  
  212. //-------------------------------------------------------------------------     
  213. //FUNCTION:                                                                     
  214. //    
  215. //    void MemFreeNear(pBlock)
  216. //
  217. //PARMS IN:
  218. //
  219. //    char near *pBlock    pointer to block of memory to be freed.
  220. //    
  221. //DESCRIPTION:
  222. //
  223. //    Uses Windows Local allocation functions to unlock and free an 
  224. //    allocated block of memory.  
  225. //    
  226. //-------------------------------------------------------------------------
  227.  
  228.  
  229. void
  230. MemFreeNear(char near *p)
  231. {
  232.     HANDLE         h;
  233.  
  234.     h = LocalHandle((WORD)p);
  235.     LocalUnlock(h);
  236.     LocalFree(h);
  237. }
  238.  
  239.